// packagMy/pages/partnerLogin/partnerLogin.js
var configUrl = require('../../../config/config')  //引入配置接口文件
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    password:true,
    isShow:'',
    showSelectBox:false,
    dealerShow: false,
    storeShow: false,
    actions: [
      {
        name: '总部职员',
        value:1
      },
      {
        name: '门店职员',
        value:2
      },
    ],
    dealerData: [],
    dealer: '',
    storeData: [],
    belongStore: '',
    storeSubmit: '',
    selectStore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#ffffff'
    })
    wx.setNavigationBarTitle({
      title: '推广身份注册',
    })
  },
  lookPwd:function(e){
    if(this.data.password){
      this.setData({
        password:false,
      })
    }else{
      this.setData({
        password:true
      })
    }
    
  },
  selectCode(e){  //打开选择框
    this.setData({ showSelectBox:true })
    
  },
  onClose() {  //关闭选择
    this.setData({ showSelectBox: false });
  },
  onSelect(e){
    let value = e.detail.value
    console.log(value)
    this.setData({userStatus:e.detail})
    if (value == 2) {
      this.setData({
       selectDealer: true,
      })
     } else {
       this.setData({
         selectDealer: false,
         selectStore: false
        })
     }
  },
  getPhoneNum:function(e){  //手机号
    this.setData({
      phoneNum:e.detail
    })
  },
  clearPhoneNum:function(e){  //清空输入框
    this.setData({
      phoneNum:''
    })
  },
  getPassword:function(e){    //密码
    if(e.detail ==""){
      this.setData({
        isShow:''
      })
    }else{
      this.setData({
        isShow:'eye-o'
      })
    }
    this.setData({
      Password:e.detail
    })
  },
  dealerOnClose() {
    this.setData({
      dealerShow: false
    })
  },
  storeOnClose() {
    this.setData({
      storeShow: false
    })
  },
  selectStore() {
    this.setData({
      storeShow: true
    })
  },
  dealerChange() {
      let dealer = this.data.dealerData
    this.setData({
        dealer: dealer[0].text,
        dealerShow: false,
        dealerSubmit: dealer[0]
    })
  },
  storeChange() {
    let store = this.data.storeData
    this.setData({
        belongStore: store[0].text,
        storeShow: false,
        storeSubmit: store[0]
    })
  },
  dealerOnChange(event){
      this.setData({
        dealer:  event.detail.value.text,
        dealerSubmit: event.detail.value
      })
  },
  popuClose() {
    let dealer = this.data.dealer
    let dealerSubmit = this.data.dealerSubmit
    let dealerData = this.data.dealerData
    if (!dealer && !dealerSubmit) {
      this.setData({
          dealer: dealerData[0].text,
          dealerSubmit: dealerData[0]
      })
    }
    this.setData({
      dealerShow: false
    })
  },
  storePopuClose() {
    let belongStore = this.data.belongStore
    let storeSubmit = this.data.storeSubmit
    let storeData = this.data.storeData
    if (!belongStore && !storeSubmit) {
      this.setData({
        belongStore: storeData[0].text,
        storeSubmit: storeData[0]
      })
    }
    this.setData({
      storeShow: false
    })
  },
  storeOnChange(event){
    this.setData({
        belongStore: event.detail.value.text,
        storeSubmit: event.detail.value
    })
},
	// 选择经销商
	selectDealer() {
    let that = this
    // this.setData({
    //     dealerShow: true,
    // })
    if(!that.data.userStatus){
      wx.showToast({
        title: '请选择注册人身份',
        icon: 'none',
        mask:true,
        duration: 2000
      }) 
      return;
    }
		if(!that.data.phoneNum){
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        mask:true,
        duration: 2000
      }) 
      return;
    }
    if(!that.data.Password){
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        mask:true,
        duration: 2000
      }) 
      return;
		}
		var parms={
      userId:wx.getStorageSync('loginMsg').userId,   //用户ID
			partnerIdentity:that.data.userStatus.value,   //身份
			account:that.data.phoneNum,                  //账号
			password:that.data.Password,                  //密码      
		}
		app.reqFun.requestJson('POST',configUrl.dealerStore,parms,(res)=>{
			if(res.data.success==1){
                if (res.data.data.dis) {
                    let disNew = []
                    let dis = res.data.data.dis // 所属经销商
                    for(var i = 0, len = dis.length; i < len; i++) {
                        let obj = {}
                        obj.text = dis[i].disName
                        obj.disCode = dis[i].disCode
                        obj.staffCode = dis[i].staffCode
                        obj.staffName = dis[i].staffName
                        disNew.push(obj)
                    }
                    this.setData({
                        dealerData: disNew,
                        // dealer:  disNew[0].text,
                        // dealerSubmit: disNew[0]
                    })
                }
                if (res.data.data.mdmOrg) {
                    let mdmOrgNew = []
                    let mdmOrg = res.data.data.mdmOrg // 所属门店
                    for(var i = 0, len = mdmOrg.length; i < len; i++) {
                        let obj = {}
                        obj.text = mdmOrg[i].mdmOrgName
                        obj.disCode = mdmOrg[i].mdmOrgCode
                        obj.staffCode = mdmOrg[i].staffCode
                        obj.staffName = mdmOrg[i].staffName
                        mdmOrgNew.push(obj)
                    }
                    this.setData({
                        storeData: mdmOrgNew,
                        // belongStore: mdmOrgNew[0].text,
                        // storeSubmit: mdmOrgNew[0],
                        selectStore: true
                    })
                } else {
                  this.setData({
                    selectStore: false
                })
                }
				this.setData({
                    dealerShow: true,
                })
			}else{
				wx.showToast({
					icon:'none',
					title: res.data.msg,
				})
			}
		})
  },
  clearDealer() {
    this.setData({
      dealer: ''
    })
  },
  clearStore() {
    this.setData({
      belongStore: ''
    })
  },
  goregin(){   //注册提交
    var that=this;
    let dealerSubmit = that.data.dealerSubmit
    let storeSubmit = that.data.storeSubmit
    console.log('dealerSubmit', dealerSubmit, storeSubmit)
    if(!that.data.userStatus){
      wx.showToast({
        title: '请选择注册人身份',
        icon: 'none',
        mask:true,
        duration: 2000
      }) 
      return;
    }
    if(!that.data.phoneNum){
      wx.showToast({
        title: '请输入账号/手机号',
        icon: 'none',
        mask:true,
        duration: 2000
      }) 
      return;
    }
    if(!that.data.Password){
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        mask:true,
        duration: 2000
      }) 
      return;
    }
    if (that.data.selectDealer&&!that.data.dealer) {
        wx.showToast({
            title: '请选择所属经销商',
            icon: 'none',
            mask:true,
            duration: 2000
          }) 
          return;
    }
    wx.getSystemInfo({
      success (res) {
        var parms = {}
          if (storeSubmit) {
            parms={
                loginPlatform:res.system,                            //手机平台
                loginSys:res.model,                                  //手机型号
                partnerIdentity:that.data.userStatus.value,   //身份
                account:that.data.phoneNum,                  //账号
                password:that.data.Password,                  //密码      
                userId:wx.getStorageSync('loginMsg').userId,   //用户ID
                dealerCode: dealerSubmit.disCode,
                dealerName: dealerSubmit.text,
                staffCode: dealerSubmit.staffCode,
                staffName: dealerSubmit.staffName,
                storeCode: storeSubmit.disCode,
                storeName: storeSubmit.text
              }
          } else if(dealerSubmit) {
            parms={
                loginPlatform:res.system,                            //手机平台
                loginSys:res.model,                                  //手机型号
                partnerIdentity:that.data.userStatus.value,   //身份
                account:that.data.phoneNum,                  //账号
                password:that.data.Password,                  //密码      
                userId:wx.getStorageSync('loginMsg').userId,   //用户ID
                dealerCode: dealerSubmit.disCode,
                dealerName: dealerSubmit.text,
                staffCode: dealerSubmit.staffCode,
                staffName: dealerSubmit.staffName
              }
          } else {
            parms={
              loginPlatform:res.system,                            //手机平台
              loginSys:res.model,                                  //手机型号
              partnerIdentity:that.data.userStatus.value,   //身份
              account:that.data.phoneNum,                  //账号
              password:that.data.Password,                  //密码      
              userId:wx.getStorageSync('loginMsg').userId,   //用户ID
            }
          }
          // console.log('parms', parms)
        app.reqFun.requestJson('POST',configUrl.partnerRegister,parms,(res)=>{
          if(res.data.success==1){
            console.log(1)
            wx.showToast({
              title: '注册成功',
              duration:3000,
            })   
          that.getUser()  //调用初始化登录刷新缓存用户数据
                 
          }else{
            wx.showToast({
              icon:'none',
              title: res.data.msg,
            })
          }
        })
      }     
    }) 
  },
  async getUser(){  //跳转合伙人主页
    await app.initLogin()
    console.log(2)
    wx.redirectTo({
      url: '../partner/partner'
    })
    // setInterval(()=>{
    //   if(wx.getStorageSync('loginMsg').partner){
    //     wx.redirectTo({
    //       url: '../partner/partner'
    //     })
    //   }
    // },500)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})